package com.mbc.hr.manager;

import java.math.BigInteger;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;

import com.mbc.common.data.DataList;
import com.mbc.common.manager.AbstractMgr;
import com.mbc.common.param.CriteriaParams;
import com.mbc.hr.entities.FingerPrintDevice;

public class FingerPrintDeviceMgr extends AbstractMgr {
	public FingerPrintDeviceMgr(){}
	
	public FingerPrintDeviceMgr(String sessionId){
		this.sessionId = sessionId;
	}
	
	public static long generateFingerPrintID(EntityManager em)throws Exception{
		return ((BigInteger)em.createStoredProcedureQuery("sp_hr_fpid_generate").getSingleResult()).longValue();
	}
	
	public void create(FingerPrintDevice device)throws Exception{
		openConnection();
		try {
			beginTransaction();
			entityManager.persist(device);
			commitTransaction();
		} catch (Exception e) {
			rollbackTransaction();
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public void update(FingerPrintDevice device)throws Exception{
		openConnection();
		try {
			beginTransaction();
			entityManager.merge(device);
			commitTransaction();
		} catch (Exception e) {
			rollbackTransaction();
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public void delete(long id)throws  Exception{
		openConnection();
		try {
			beginTransaction();
			entityManager.createNamedQuery("FingerPrintDevice.delete")
			.setParameter("deviceID", id)
			.executeUpdate();
			commitTransaction();
		} catch (Exception e) {
			rollbackTransaction();
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public List<FingerPrintDevice> getAll()throws Exception{
		openConnection();
		try {
			return entityManager.createNamedQuery("FingerPrintDevice.findAll", FingerPrintDevice.class).getResultList();
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public FingerPrintDevice find(long id)throws Exception{
		openConnection();
		try {
			return entityManager.find(FingerPrintDevice.class, id);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public FingerPrintDevice find(String ipAddress)throws Exception{
		FingerPrintDevice l_device = null;
		openConnection();
		try {
			l_device = entityManager.createNamedQuery("FingerPrintDevice.findByIP", FingerPrintDevice.class)
			.setParameter("ipAddress", ipAddress)
			.getSingleResult();
		} catch (NoResultException e){
			
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
		return l_device;
	}
	
	public DataList<FingerPrintDevice> searchDevices(CriteriaParams cri)throws Exception{
		DataList<FingerPrintDevice> l_dataList = new DataList<FingerPrintDevice>();
		openConnection();
		try {
			String l_cri = " where fpd.deviceName like '%" + cri.getKeyword() + "%' or fpd.ipAddress like '%" + cri.getKeyword() + "%'";
            
            String l_searchJPQL = "select fpd from FingerPrintDevice fpd" + l_cri;
            List<FingerPrintDevice> l_entityList = entityManager.createQuery(l_searchJPQL, FingerPrintDevice.class)
                    .setFirstResult(cri.getIndex())
                    .setMaxResults(cri.getSize())
                    .getResultList();
            
            l_dataList.setEntityList(l_entityList);
            
            if(cri.getIndex() == 0){
            	String l_countQuery = "select count(fpd.deviceID) from FingerPrintDevice fpd" + l_cri;
            	l_dataList.setTotalRecord(entityManager.createQuery(l_countQuery, Long.class).getSingleResult());
            }
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
		return l_dataList;
	}
}
